//## 오더  정보 저장 ####################
function fnSave()
{
	if (!$("#compCd").val())
	{
		alert("거래처를 선택해 주세요!");
		$("#compNm").focus();
		return;
	}
	if (!$("#major").val())
	{
		alert("담당자를 바르게 입력해 주세요!");
		$("#major").focus();
		return;
	}
	var valUrl  = '/action/order/save.asp';
		var param = "orderNo="+$("#orderNo").val();
		param += "&major="+encodeURIComponent($("#major").val());
		param += "&majorPhone="+encodeURIComponent($("#majorPhone").val());
		param += "&compOrderNo="+$("#compOrderNo").val();
		param += "&orderType="+encodeURIComponent($("#orderType").val());
		param += "&orderDt="+$("#orderDt").val();
		param += "&totalPrice="+$("#totalPrice").val();
		param += "&orderMemo="+encodeURIComponent($("#orderMemo").val());
		param += "&compCd="+$("#compCd").val();
		param += "&flag="+$("#flag").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				grid01List();
				fnReset();
				setTimeout("fnShowOrder('"+$(xml).find("VAL").text()+"');",500);
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 오더 품목 정보 저장 ####################
function fnSaveArticle()
{
	if (!$("#orderNo").val())
	{
		alert("오더를 먼저 선택해 주세요!");
		return;
	}
	if (!$("#compPrdCd").val())
	{
		alert("자재코드를 바르게 입력해 주세요!");
		$("#compPrdCd").focus();
		return;
	}
	var valUrl  = '/action/order/saveArticle.asp';
		var param = "orderNo="+$("#orderNo").val();
		param += "&compPrdCd="+encodeURIComponent($("#compPrdCd").val());
		param += "&compPrdNm="+encodeURIComponent($("#compPrdNm").val());
		param += "&compPrdSize="+encodeURIComponent($("#compPrdSize").val());
		param += "&amount="+$("#amount").val();
		param += "&unitPrice="+$("#unitPrice").val();
		param += "&articlePrice="+$("#articlePrice").val();
		param += "&dueDt="+$("#dueDt").val();
		param += "&flag="+$("#articleFlag").val();
		param += "&seq="+$("#seq").val();

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				setTimeout("fnShowOrder('"+$(xml).find("VAL").text()+"');",500);
				fnResetArticle();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}


//## 오더 정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").css("display","");
	setNullRow("loadSpan","1");
	var valUrl = "/action/order/list.asp";
	var param = "findPrdNm="+encodeURIComponent($("#findPrdNm").val());
		  param += "&findPrdCd="+encodeURIComponent($("#findPrdCd").val());
		  param += "&findOrderNo="+encodeURIComponent($("#findOrderNo").val());
		  param += "&findCompany="+encodeURIComponent($("#findCompany").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Orders").find("Order").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var orderNo		= $(G_grid01Rslt).find("Orders").find("Order").eq(j).find("orderNo").text();
		var company		= $(G_grid01Rslt).find("Orders").find("Order").eq(j).find("company").text();
		var major		= $(G_grid01Rslt).find("Orders").find("Order").eq(j).find("major").text();
		var compOrderNo		= $(G_grid01Rslt).find("Orders").find("Order").eq(j).find("compOrderNo").text();
		var orderDt		= $(G_grid01Rslt).find("Orders").find("Order").eq(j).find("orderDt").text();
		var flag				= $(G_grid01Rslt).find("Orders").find("Order").eq(j).find("flag").text();

		ji = j+1;
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "유효";
		else showFlag = "삭제";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else var bgColor = "#FFFFFF";

		grid01Array[0] = orderNo;
		grid01Array[1] = tagFirst + company + tagLast;
		grid01Array[2] = tagFirst + major + tagLast;
		grid01Array[3] = tagFirst + compOrderNo + tagLast;
		grid01Array[4] = tagFirst + orderDt + tagLast;
		grid01Array[5] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",left,,,,,", "fnShowOrder('"+orderNo+"')", boldLine);
	}
}


//## 오더 상세정보 표시 ####################
function fnShowOrder(orderNo)
{
	var valUrl = "/action/order/view.asp";
	var param = "orderNo="+orderNo;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();			
			$("#orderNo").val($(xml).find("orderNo").text());
			$("#major").val($(xml).find("major").text());
			$("#majorPhone").val($(xml).find("majorPhone").text());
			$("#compOrderNo").val($(xml).find("compOrderNo").text());
			$("#orderType").val($(xml).find("orderType").text());
			$("#orderDt").val($(xml).find("orderDt").text());
			$("#orderMemo").val($(xml).find("orderMemo").text());
			$("#totalPrice").val($(xml).find("totalPrice").text());
			$("#flag").val($(xml).find("flag").text());
			fnFindCompany('',$(xml).find("compCd").text(),'compCd');	
			grid02List();

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 오더 품목 그리드 표시 ####################

var G_grid02Rslt = "" //XML Data를 받을 전역변수 선언

function grid02List()
{
	clstTable("Grid02");
	$("#tr02Result").css("display","");
	setNullRow("loadSpan","2");
	var ordNo = $("#orderNo").val();
	if (!ordNo)  ordNo = 0;
	var valUrl = "/action/order/listArticle.asp";
	var param = "orderNo="+ordNo;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid02Rslt = xml;
			setGrid02(1);
			fnResetArticle();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid02(curPage)
{
	clstTable("Grid02");

	$("#tr02Result").css("display","none");
	$("#loadSpan2").html("");
	var nodeCnt = $(G_grid02Rslt).find("OrderArticle").find("Article").length;

	if(nodeCnt == 0)
	{
		$("#tr02Result").css("display","");
		$("#loadSpan2").html("검색된 자료가 없습니다.");
		$("#grid02Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid02Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid02", curPage, totalPage);

	$("#pageDiv02").html(pages);
	
	var grid02Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var compPrdCd		= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("compPrdCd").text();
		var compPrdNm	= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("compPrdNm").text();
		var articlePrice		= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("articlePrice").text();
		var dueDt				= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("dueDt").text();
		var amount			= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("amount").text();
		var flag					= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("flag").text();
		var seq					= $(G_grid02Rslt).find("OrderArticle").find("Article").eq(j).find("seq").text();

		ji = j+1;
		
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "유효";
		else if(flag == "E") showFlag = "완료";
		else showFlag = "삭제";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else var bgColor = "#FFFFFF";

		grid02Array[0] = ji;
		grid02Array[1] = tagFirst + compPrdCd + tagLast;
		grid02Array[2] = tagFirst + compPrdNm + tagLast;
		grid02Array[3] = tagFirst + amount + tagLast;
		grid02Array[4] = tagFirst + articlePrice + tagLast;
		grid02Array[5] = tagFirst + dueDt + tagLast;
		grid02Array[6] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid02", grid02Array, bgColor, ",left,,,,,", "fnShowOrderArticle('"+seq+"')", boldLine);
	}
}

//## 오더 품목 상세 정보
function fnShowOrderArticle(seq)
{
	var valUrl = "/action/order/viewArticle.asp";
	var param = "seq="+seq;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnResetArticle();			
			$("#seq").val($(xml).find("seq").text());
			$("#compPrdCd").val($(xml).find("compPrdCd").text());
			$("#compPrdNm").val($(xml).find("compPrdNm").text());
			$("#compPrdSize").val($(xml).find("compPrdSize").text());
			$("#amount").val($(xml).find("amount").text());
			$("#unitPrice").val($(xml).find("unitPrice").text());
			$("#articlePrice").val($(xml).find("articlePrice").text());
			$("#dueDt").val($(xml).find("dueDt").text());
			$("#articleFlag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});

}

//##  오더 품목 금액 구하기
function fnGetOrderTotal()
{
	var amt = $("#amount").val();
	amt = amt.split(",").join("");
	var unitPrice = $("#unitPrice").val();
	unitPrice = unitPrice.split(",").join("");
	if (!amt) amt = 0;	
	if (!unitPrice) unitPrice = 0;
	var articlePrice = parseInt(amt,10)*parseInt(unitPrice,10);
	$("#articlePrice").val(getNumWithComma(articlePrice));
}

//## 오더  정보 Reset 기능
function fnReset()
{
	$("#orderNo").val("");
	$("#compCd").val("");
	$("#compNm").val("");
	$("#major").val("");
	$("#compOrderNo").val("");
	$("#majorPhone").val("");
	$("#orderType").val("");
	$("#orderMemo").val("");
	$("#totalPrice").val("");
	$("#flag").val("Y");
	clstTable("Grid02");
}

//## 오더 품목 정보 Reset 기능
function fnResetArticle()
{
	$("#compPrdCd").val("");
	$("#compPrdNm").val("");
	$("#compPrdSize").val("");
	$("#amount").val("");
	$("#unitPrice").val("");
	$("#articlePrice").val("");
	$("#seq").val("");
	$("#articleFlag").val("Y");
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findOrderNo").val("");
	$("#findCompany").val("");
	$("#findPrdCd").val("");
	$("#findPrdNm").val("");
}